import { AddEquation, AmbientLight, BoxGeometry, CustomBlending, DirectionalLight, DoubleSide, HemisphereLight, LinearFilter, Mesh, MeshBasicMaterial, MeshLambertMaterial, MeshPhongMaterial, MeshStandardMaterial, NoBlending, NormalBlending, Object3D, PerspectiveCamera, PlaneGeometry, SRGBColorSpace, SpotLight, SpotLightHelper, TextureLoader, Vector2, Vector3, VideoTexture } from "three";
import { App, Point } from "../Engine";
import { EventsDispatch } from "./EventsDispatch";
// import { ProjectedMaterial } from '@lume/three-projected-material/dist/ProjectedMaterial.js'
/**
 * 灯光对象
 */
class Light extends EventsDispatch {

    private app: App;
    /**
     * 环境光
     */
    public ambientLight: AmbientLight | HemisphereLight;
    /**
     * 辅助平行光1
     */
    public light1: DirectionalLight;
    /**
     * 辅助平行光2
     */
    public light2: DirectionalLight;
    /**
     * 辅助平行光3
     */
    public light3: DirectionalLight;
    private lightHeight: number;

    constructor() {
        super();
        this.app = App.getInstance();

        this.lightHeight = this.app.options.Light.lightHeight;
        this.initAmbientLight();
        this.initSupportLights();

    }

    private initAmbientLight() {
        let scene = this.app.three.scene;
        switch (this.app.options.Light.ambientLightType) {
            case 'ambient': {
                this.ambientLight = new AmbientLight(this.app.options.Light.ambientLightColor, this.app.options.Light.ambientLightIntensity);
                scene.add(this.ambientLight);
                break;
            }
            case 'hemiSphere': {
                this.ambientLight = new HemisphereLight(0xffffff, this.app.options.Light.ambientLightColor, this.app.options.Light.ambientLightIntensity);
                this.ambientLight.position.set(0, 0, this.lightHeight);
                this.ambientLight.up = new Vector3(0, 0, 1);

                scene.add(this.ambientLight);
                break;
            }
            default: {
                this.ambientLight = new HemisphereLight(0xffffff, this.app.options.Light.ambientLightColor, this.app.options.Light.ambientLightIntensity);
                this.ambientLight.position.set(0, 0, this.lightHeight);
                scene.add(this.ambientLight);
                break;
            }
        }
    }

    private initSupportLights() {
        let scene = this.app.three.scene;

        let supRotation = 0;

        let radius = supRotation / 180 * Math.PI;
        let distance = this.app.options.Light.lightDistance;
        let x = Math.sin(radius) * distance;
        let y = Math.cos(radius) * distance;

        let radius2 = (supRotation - 90) / 180 * Math.PI;
        let x2 = Math.sin(radius2) * distance;
        let y2 = Math.cos(radius2) * distance;

        switch (this.app.options.Light.supportLightType) {
            case 'single': {
                if (!this.light1) {
                    this.light1 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light1);
                }
                this.light1.intensity = this.app.options.Light.supportLightIntensity;
                this.light1.color.set(this.app.options.Light.supportLightColor);
                this.light1.position.set(x, y, this.lightHeight);

                if (this.light2) {
                    scene.remove(this.light2);
                    this.light2.dispose();
                    this.light2 = null;
                }

                if (this.light3) {
                    scene.remove(this.light3);
                    this.light3.dispose();
                    this.light3 = null;
                }
                break;
            }
            case 'double': {
                if (!this.light1) {
                    this.light1 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light1);
                }
                this.light1.intensity = this.app.options.Light.supportLightIntensity;
                this.light1.color.set(this.app.options.Light.supportLightColor);
                this.light1.position.set(x, y, this.lightHeight);

                if (!this.light2) {
                    this.light2 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light2);
                }

                this.light2.intensity = this.app.options.Light.supportLightIntensity;
                this.light2.color.set(this.app.options.Light.supportLightColor);
                this.light2.position.set(x2, y2, this.lightHeight);

                if (this.light3) {
                    scene.remove(this.light3);
                    this.light3.dispose();
                    this.light3 = null;
                }
                break;
            }
            case 'tripple': {
                if (!this.light1) {
                    this.light1 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light1);
                }

                this.light1.intensity = this.app.options.Light.supportLightIntensity;
                this.light1.color.set(this.app.options.Light.supportLightColor);
                this.light1.position.set(x, y, this.lightHeight);

                if (!this.light2) {
                    this.light2 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light2);
                }

                this.light2.intensity = this.app.options.Light.supportLightIntensity;
                this.light2.color.set(this.app.options.Light.supportLightColor);
                this.light2.position.set(x2, y2, this.lightHeight);

                if (!this.light3) {
                    this.light3 = new DirectionalLight(this.app.options.Light.supportLightColor, this.app.options.Light.supportLightIntensity);
                    scene.add(this.light3);
                }

                this.light3.intensity = this.app.options.Light.supportLightIntensity;
                this.light3.color.set(this.app.options.Light.supportLightColor);
                this.light3.position.set(x2, y2, this.lightHeight);
                break;
            }
            default:
                return;
        }
    }
}
export { Light }